<script setup lang="ts">
import { ref, onMounted, onUnmounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ArrowDown, Lock } from '@element-plus/icons-vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import ajax from "../main.ts"

const router = useRouter()
const isDropdownVisible = ref(false)
const username = ref('用户123456789')
const selectedAgent = ref('智能体1')
const agents = [
  { value: 'agent1', label: '智能体1' },
  { value: 'agent2', label: '智能体2' }
]

// 修改密码相关
const showPasswordDialog = ref(false)
const passwordForm = reactive({
  newPassword: '',
  confirmPassword: ''
})
const passwordFormRules = {
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { 
      validator: (rule: any, value: string, callback: any) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      }, 
      trigger: 'blur' 
    }
  ]
}
const passwordFormRef = ref()

// 打开修改密码弹窗
const openPasswordDialog = () => {
  isDropdownVisible.value = false // 关闭下拉菜单
  showPasswordDialog.value = true
}

// 提交修改密码
const submitPasswordChange = async () => {
  try {
    await passwordFormRef.value.validate()
    
    // 调用修改密码接口
    try {
      const response = await ajax.post('changePassword/', {
        newPassword: passwordForm.newPassword
      })
      
      if (response.data.code === 200) {
        ElMessage.success('密码修改成功')
        showPasswordDialog.value = false
        passwordForm.newPassword = ''
        passwordForm.confirmPassword = ''
      } else {
        throw new Error(response.data.msg || '修改密码失败')
      }
    } catch (error) {
      console.error('修改密码错误:', error)
      ElMessage.error('修改密码失败，请稍后重试')
    }
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}

// 关闭修改密码弹窗
const cancelPasswordChange = () => {
  showPasswordDialog.value = false
  passwordForm.newPassword = ''
  passwordForm.confirmPassword = ''
}

onMounted(() => {
  // 获取用户名
  const storedUsername = localStorage.getItem('username')
  if (storedUsername) {
    username.value = storedUsername
  }
  
  // 添加点击事件监听
  document.addEventListener('click', closeDropdown)
})

const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 调用退出登录接口
    ajax.post('logOut/')
      .then(response => {
        if (response.data.code === 200) {
          // 清除本地存储的用户信息
          localStorage.removeItem('userid')
          localStorage.removeItem('token')
          // 跳转到登录页
          router.push('/login')
        } else {
          throw new Error(response.data.msg || '退出登录失败')
        }
      })
      .catch(error => {
        console.error('退出登录错误:', error)
        // 即使请求失败，也清除本地存储并跳转到登录页
        localStorage.removeItem('userid')
        localStorage.removeItem('token')
        router.push('/login')
      })
  }).catch(() => {
    // 用户取消操作，不做任何处理
  })
}

const toggleDropdown = () => {
  isDropdownVisible.value = !isDropdownVisible.value
}

// 点击其他地方关闭下拉菜单
const closeDropdown = (e: MouseEvent) => {
  const target = e.target as HTMLElement
  if (!target.closest('.user-profile')) {
    isDropdownVisible.value = false
  }
}

// 组件卸载时移除事件监听
onUnmounted(() => {
  document.removeEventListener('click', closeDropdown)
})
</script>

<template>
  <div class="main-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="header-content">
        <div class="header-left">
          <router-link to="/" class="logo-link">
            <h1 class="title">MCP CLIENT</h1>
          </router-link>
        </div>
        <div class="header-right">
          <div class="user-profile" @click="toggleDropdown">
            <img 
              src="https://air-apis.oss-cn-shenzhen.aliyuncs.com/avatars/apitou.jpg" 
              alt="用户头像" 
              class="user-avatar"
            />
            <span class="username">{{ username }}</span>
            <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
            <div class="dropdown-menu" v-show="isDropdownVisible">
              <div class="dropdown-item" @click="openPasswordDialog">
                <i class="el-icon-lock"></i>
                修改密码
              </div>
              <div class="dropdown-item" @click="handleLogout">
                <i class="el-icon-switch-button"></i>
                注销
              </div>
            </div>
          </div>
        </div>
      </div>
    </header>
    <!-- 主体内容 -->
    <div class="main-content">
      <div class="content-wrapper">
        <router-view/>
      </div>
    </div>

    <!-- 修改密码弹窗 -->
    <el-dialog
      v-model="showPasswordDialog"
      title="修改密码"
      width="400px"
      custom-class="password-dialog"
      :show-close="true"
      :close-on-click-modal="false"
      :close-on-press-escape="true"
    >
      <el-form
        ref="passwordFormRef"
        :model="passwordForm"
        :rules="passwordFormRules"
        label-position="top"
      >
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="passwordForm.newPassword"
            type="password"
            placeholder="请输入新密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="passwordForm.confirmPassword"
            type="password"
            placeholder="请再次输入新密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancelPasswordChange">取消</el-button>
          <el-button type="primary" @click="submitPasswordChange" class="submit-btn">
            确认修改
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
/* 隐藏滚动条但保持滚动功能 */
::-webkit-scrollbar {
  display: none;
}

/* Firefox */
* {
  scrollbar-width: none;
}

.main-container {
  height: 98vh;
  background: linear-gradient(135deg, #ffffff 0%, #fcfcfc 50%, #fafafa 100%);
  background-attachment: fixed;
  width: 100%;
  position: relative;
  overflow: hidden;
  -ms-overflow-style: none; /* IE and Edge */
  display: flex;
  flex-direction: column;
}

.main-container::before {
  content: "";
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    radial-gradient(circle at 20% 35%, rgba(230, 225, 220, 0.025) 0%, transparent 50%),
    radial-gradient(circle at 75% 44%, rgba(220, 215, 210, 0.025) 0%, transparent 40%),
    url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23CCCCCC' fill-opacity='0.12'%3E%3Cpath d='M60 56.7v-6.7h-3.3v6.7h-6.7v3.3h6.7v6.7h3.3V60h6.7v-3.3h-6.7zm0-50V0h-3.3v6.7h-6.7v3.3h6.7v6.7h3.3v-6.7h6.7V6.7h-6.7zM10 56.7v-6.7H6.7v6.7H0v3.3h6.7v6.7h3.3V60h6.7v-3.3h-6.7zM10 6.7V0H6.7v6.7H0v3.3h6.7v6.7h3.3v-6.7h6.7V6.7h-6.7z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
  z-index: 0;
  opacity: 0.95;
  pointer-events: none;
}

.main-container::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  backdrop-filter: blur(0.3px);
  pointer-events: none;
  z-index: 0;
}

.header {
  background: rgba(255, 255, 255, 0.95);
  color: #333;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 100;
  height: 64px;
}

.header-content {
  width: 100%;
  padding: 0 48px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo-link {
  text-decoration: none;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.title {
  font-size: 20px;
  font-weight: 800;
  margin: 0;
  letter-spacing: 1px;
  background: linear-gradient(90deg, #6366f1, #8b5cf6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  color: transparent;
  transition: filter 0.2s ease;
}

.logo-link:hover .title {
  filter: brightness(1.1) saturate(1.2);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.usage-link {
  color: #6366f1;
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  transition: color 0.3s ease;
}

.usage-link:hover {
  color: #4f46e5;
  text-decoration: underline;
}

.agent-select {
  width: 180px;
  margin-right: 16px;
  position: relative;
}

.agent-select :deep(.el-input__wrapper) {
  background: rgba(255, 255, 255, 0.7);
  box-shadow: 0 4px 20px rgba(99, 102, 241, 0.15);
  border: 1px solid rgba(99, 102, 241, 0.2);
  border-radius: 16px;
  padding: 0 20px;
  height: 44px;
  transition: all 0.4s cubic-bezier(0.645, 0.045, 0.355, 1);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  position: relative;
  overflow: hidden;
}

.agent-select :deep(.el-input__wrapper::before) {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, 
    rgba(99, 102, 241, 0.1) 0%,
    rgba(139, 92, 246, 0.1) 50%,
    rgba(99, 102, 241, 0.1) 100%
  );
  opacity: 0;
  transition: opacity 0.4s ease;
}

.agent-select :deep(.el-input__wrapper:hover) {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(99, 102, 241, 0.2);
  border-color: rgba(99, 102, 241, 0.4);
  background: linear-gradient(135deg, 
    rgba(255, 255, 255, 0.8) 0%,
    rgba(255, 255, 255, 0.7) 100%
  );
}

.agent-select :deep(.el-input__wrapper.is-focus) {
  transform: translateY(-2px);
  box-shadow: 0 12px 32px rgba(99, 102, 241, 0.25);
  border-color: #6366f1;
  background: linear-gradient(135deg, 
    rgba(255, 255, 255, 0.9) 0%,
    rgba(255, 255, 255, 0.8) 100%
  );
}

.agent-select :deep(.el-input__inner) {
  color: #1a1a1a;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  background: transparent;
}

.agent-select :deep(.el-select__caret) {
  color: #6366f1;
  font-size: 18px;
  transition: all 0.4s cubic-bezier(0.645, 0.045, 0.355, 1);
  position: relative;
  z-index: 1;
}

:deep(.agent-select-dropdown) {
  border: none;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.15);
  border-radius: 20px;
  padding: 16px;
  background: linear-gradient(135deg, 
    rgba(255, 255, 255, 0.98) 0%,
    rgba(255, 255, 255, 0.95) 100%
  );
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  overflow: hidden;
  transform-origin: top center;
  animation: dropdownAppear 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
}

@keyframes dropdownAppear {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(-10px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

:deep(.agent-select-dropdown::before) {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, 
    transparent,
    rgba(99, 102, 241, 0.3),
    transparent
  );
}

:deep(.agent-select-dropdown .el-select-dropdown__item) {
  padding: 14px 24px;
  font-size: 15px;
  color: #4b5563;
  border-radius: 12px;
  transition: all 0.4s cubic-bezier(0.645, 0.045, 0.355, 1);
  margin: 6px 0;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  gap: 12px;
}

:deep(.agent-select-dropdown .el-select-dropdown__item::before) {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, 
    rgba(99, 102, 241, 0.1),
    rgba(139, 92, 246, 0.1)
  );
  opacity: 0;
  transition: opacity 0.4s ease;
}

:deep(.agent-select-dropdown .el-select-dropdown__item:hover) {
  transform: translateX(8px);
  color: #6366f1;
}

:deep(.agent-select-dropdown .el-select-dropdown__item.selected) {
  background: linear-gradient(135deg, 
    rgba(99, 102, 241, 0.15),
    rgba(139, 92, 246, 0.15)
  );
  color: #6366f1;
  font-weight: 600;
  transform: translateX(8px);
}

:deep(.agent-select-dropdown .el-select-dropdown__item.selected::after) {
  content: '';
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  width: 8px;
  height: 8px;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  border-radius: 50%;
  box-shadow: 0 0 12px rgba(99, 102, 241, 0.6);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.4);
  }
  70% {
    box-shadow: 0 0 0 10px rgba(99, 102, 241, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(99, 102, 241, 0);
  }
}

:deep(.agent-select-dropdown .el-scrollbar__bar) {
  background-color: rgba(99, 102, 241, 0.1);
  border-radius: 6px;
  width: 6px;
}

:deep(.agent-select-dropdown .el-scrollbar__thumb) {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  border-radius: 6px;
  opacity: 0.8;
  transition: opacity 0.3s ease;
}

.main-content {
  flex: 1;
  padding: 40px 48px 24px 1px;
  position: relative;
  z-index: 1;
  overflow: hidden;
  -ms-overflow-style: none;
  width: 100%;
  height: calc(100vh - 64px);
}

.content-wrapper {
  background-color: transparent;
  border-radius: 16px;
  padding: 20px;
  height: 100%;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  overflow: hidden;
  -ms-overflow-style: none;
}

.content-wrapper::after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(135deg, rgba(255, 255, 255, 0.08) 0%, rgba(255, 255, 255, 0.03) 100%);
  pointer-events: none;
}

/* 确保在移动端也有好的显示效果 */
@supports not (backdrop-filter: blur(8px)) {
  .content-wrapper {
    background-color: rgba(255, 255, 255, 0.7);
  }
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .header-content {
    padding: 0 24px;
    height: 56px;
  }

  .title {
    font-size: 18px;
  }

  .usage-link {
    font-size: 13px;
  }

  .main-content {
    padding: 72px 24px 16px;
    height: calc(100vh - 56px);
  }

  .content-wrapper {
    padding: 20px;
    border-radius: 12px;
  }

  .username {
    display: none;
  }
  
  .user-profile {
    padding: 4px;
  }
  
  .dropdown-menu {
    right: -8px;
  }
}

.user-profile {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 12px;
  border-radius: 6px;
  transition: background-color 0.3s ease;
  margin-right: 70px;
}

.user-profile:hover {
  background-color: rgba(99, 102, 241, 0.1);
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  object-fit: cover;
  border: 2px solid #6366f1;
}

.username {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.dropdown-icon {
  font-size: 12px;
  color: #6366f1;
  transition: transform 0.3s ease;
  margin-left: 4px;
}

.user-profile:hover .dropdown-icon {
  transform: rotate(180deg);
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 8px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  min-width: 160px;
  z-index: 1000;
  overflow: hidden;
}

.dropdown-item {
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #333;
  font-size: 14px;
  transition: background-color 0.3s ease;
}

.dropdown-item:hover {
  background-color: rgba(99, 102, 241, 0.1);
  color: #6366f1;
}

.dropdown-item i {
  font-size: 16px;
}

/* 修改密码弹窗样式 */
:deep(.password-dialog) {
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(99, 102, 241, 0.1);
  background: linear-gradient(135deg, 
    rgba(255, 255, 255, 0.98) 0%,
    rgba(255, 255, 255, 0.95) 100%
  );
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

:deep(.password-dialog .el-dialog__header) {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid rgba(99, 102, 241, 0.1);
  position: relative;
}

:deep(.password-dialog .el-dialog__header::after) {
  content: '';
  position: absolute;
  bottom: 0;
  left: 15%;
  right: 15%;
  height: 1px;
  background: linear-gradient(90deg, 
    transparent,
    rgba(99, 102, 241, 0.3),
    transparent
  );
}

:deep(.password-dialog .el-dialog__title) {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  background: linear-gradient(90deg, #6366f1, #8b5cf6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

:deep(.password-dialog .el-dialog__body) {
  padding: 30px 30px 10px;
}

:deep(.password-dialog .el-dialog__footer) {
  padding: 0 30px 24px;
  border-top: none;
}

:deep(.password-dialog .el-form-item__label) {
  font-weight: 500;
  color: #555;
  margin-bottom: 6px;
}

:deep(.password-dialog .el-input__wrapper) {
  background: rgba(255, 255, 255, 0.7);
  box-shadow: 0 4px 16px rgba(99, 102, 241, 0.1);
  border: 1px solid rgba(99, 102, 241, 0.2);
  border-radius: 12px;
  padding: 0 16px;
  height: 44px;
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

:deep(.password-dialog .el-input__wrapper:hover) {
  border-color: rgba(99, 102, 241, 0.4);
  box-shadow: 0 4px 20px rgba(99, 102, 241, 0.15);
}

:deep(.password-dialog .el-input__wrapper.is-focus) {
  border-color: #6366f1;
  box-shadow: 0 4px 20px rgba(99, 102, 241, 0.2);
}

:deep(.password-dialog .el-input__inner) {
  color: #333;
  font-size: 15px;
}

:deep(.password-dialog .el-button) {
  border-radius: 10px;
  font-weight: 500;
  padding: 10px 20px;
  transition: all 0.3s ease;
}

:deep(.password-dialog .el-button--default) {
  border-color: #ddd;
  color: #555;
}

:deep(.password-dialog .el-button--default:hover) {
  background-color: #f5f5f5;
  border-color: #ccc;
}

:deep(.password-dialog .el-button--primary) {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  border: none;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

:deep(.password-dialog .el-button--primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(99, 102, 241, 0.4);
  filter: brightness(1.05);
}

:deep(.password-dialog .el-button--primary:active) {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3);
}

:deep(.password-dialog .el-input__prefix) {
  color: #6366f1;
  margin-right: 8px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 10px;
}

.submit-btn {
  min-width: 100px;
}
</style>